<?php

namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RoomWealthStream extends BaseModel
{
    protected $table = 'room_wealth_stream';
    public $timestamps = false;

    public function getAllCabinWealthWater(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $room_uid = $request->input('room_uid') ?? '';
        $where = ' WHERE business_type = 1 ';
        if ($room_uid) $where .= " AND room_uid = $room_uid";
        $date = date('Y-m-d');
        $upDay = date('Y-m-d', strtotime("$date -1 day"));
        $upWeek = date('Y-m-d', strtotime("$date -7 day"));
        $weekTime = getAWeekTimeSlotByYmd($date);//本周
        $upweekTime = getAWeekTimeSlotByYmd($upWeek);//上周
        $monthTime[] = date('Y-m-01', time());
        $monthTime[] = date('Y-m-t', time());
        $upMonthTime[] = date('Y-m-01', strtotime("$date -1 month"));
        $upMonthTime[] = date('Y-m-t', strtotime("$date -1 month"));

        $total = DB::connection('api')->selectOne("SELECT COUNT(*) total FROM zb_room {$where}");
        $now_page = ($page - 1) * $limit;
        $room_list = DB::connection('api')->select("SELECT room_id,room_uid,name,r.user_id,business_type,uuid FROM zb_room r LEFT JOIN zb_user u on r.user_id = u.user_id {$where} LIMIT {$now_page},{$limit}");
        $room_list = $this->objToArray($room_list);
        $room_id_arr = array_column($room_list, 'room_id');

        $room_id_str = implode(',', $room_id_arr);
        // 获取上月的数据
        $upMonthList = DB::connection('api')->select("SELECT room_id,convert(sum(total)/100,decimal(12,2)) as upmonth_sum FROM zb_finance_room_wealth_total WHERE room_id IN({$room_id_str}) AND day >='$upMonthTime[0]' AND day <= '$upMonthTime[1]' group by room_id");
        $upMonthList = $this->objToArray(array_column($upMonthList, null, 'room_id'));
        // 获取本月的数据
        $monthList = DB::connection('api')->select("SELECT room_id,convert(total/100,decimal(12,2)) sum_wealth,day as created_date FROM zb_finance_room_wealth_total WHERE room_id IN({$room_id_str}) AND day >='$monthTime[0]' AND day <= '$monthTime[1]'");
        $monthSumArr = [];
        $monthList = $this->objToArray($monthList);
        // 本月、上周、本周
        foreach ($monthList as $key => $month) {
            if (isset($monthSumArr[$month['room_id']]['month_sum'])) {
                $monthSumArr[$month['room_id']]['month_sum'] += $month['sum_wealth'];
            } else {
                $monthSumArr[$month['room_id']]['month_sum'] = 0;
                $monthSumArr[$month['room_id']]['month_sum'] += $month['sum_wealth'];
            }
            // 上周
            if ($month['created_date'] >= $upweekTime[0] && $month['created_date'] <= $upweekTime[1]) {
                if (isset($monthSumArr[$month['room_id']]['upweek_sum'])) {
                    $monthSumArr[$month['room_id']]['upweek_sum'] += $month['sum_wealth'];
                } else {
                    $monthSumArr[$month['room_id']]['upweek_sum'] = 0;
                    $monthSumArr[$month['room_id']]['upweek_sum'] += $month['sum_wealth'];
                }
            }
            // 本周
            if ($month['created_date'] >= $weekTime[0] && $month['created_date'] <= $weekTime[1]) {
                if (isset($monthSumArr[$month['room_id']]['week_sum'])) {
                    $monthSumArr[$month['room_id']]['week_sum'] += $month['sum_wealth'];
                } else {
                    $monthSumArr[$month['room_id']]['week_sum'] = 0;
                    $monthSumArr[$month['room_id']]['week_sum'] += $month['sum_wealth'];
                }
            }
            // 昨日
            if ($month['created_date'] === $upDay) {
                if (isset($monthSumArr[$month['room_id']]['day_sum'])) {
                    $monthSumArr[$month['room_id']]['day_sum'] += $month['sum_wealth'];
                } else {
                    $monthSumArr[$month['room_id']]['day_sum'] = 0;
                    $monthSumArr[$month['room_id']]['day_sum'] += $month['sum_wealth'];
                }
            }
        }

        foreach ($room_list as $k => &$v) {
            //上月
            $v['upMonthSum'] = $v['monthSum'] = $v['upweekSum'] = $v['weekSum'] = $v['daySum'] = 0;
            if (isset($upMonthList[$v['room_id']])) {
                $v['upMonthSum'] = $upMonthList[$v['room_id']]['upmonth_sum'];
            }
            if (isset($monthSumArr[$v['room_id']])) {
                $monthSum = $monthSumArr[$v['room_id']]['month_sum'] ?? 0;
                $upweekSum = $monthSumArr[$v['room_id']]['upweek_sum'] ?? 0;
                $weekSum = $monthSumArr[$v['room_id']]['week_sum'] ?? 0;
                $daySum = $monthSumArr[$v['room_id']]['day_sum'] ?? 0;
                $v['monthSum'] = round($monthSum, 2);
                $v['upweekSum'] = round($upweekSum, 2);
                $v['weekSum'] = round($weekSum, 2);
                $v['daySum'] = round($daySum, 2);
            }
        }
        return ['total' => $total, 'data' => $room_list, 'currentPage' => intval($page)];
    }


    public function getRoomMonthWealth($room_id, $month)
    {
        $where['room_id'] = $room_id;
        $whereBetween[] = date('Y-m-01', strtotime($month));
        $whereBetween[] = date('Y-m-t', strtotime($month));
        $res = DB::connection('api')->table('finance_room_wealth_total')
            ->select(DB::raw('SUM(total) as sum_total'))
            ->where($where)
            ->whereBetween('day', $whereBetween)
            ->first();
        return $this->objToArray($res);
    }

}
